package com.jsnkj.basic.adapter

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.jsnkj.basic.R
import com.jsnkj.basic.bean.ReadPacketMessageBean
import com.jsnkj.basic.databinding.RvItemRedPacketAdBinding
import com.jsnkj.basic.databinding.RvItemRedPacketImgBinding
import com.jsnkj.basic.databinding.RvItemRedPacketMsgBinding


class MessageAdapter(
    val context: Activity,
    val messages: ArrayList<ReadPacketMessageBean>,
    val click: onItemClickListener
) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): RecyclerView.ViewHolder {

        //发送消息类型 ： 0：正常消息 1：广告 2：红包
        if (0 == viewType) {
            return MessageDataViewHolder(
                DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.rv_item_red_packet_msg, parent, false
                )
            )
        } else if (1 == viewType) {
            return MessageAdViewHolder(
                DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.rv_item_red_packet_ad, parent, false
                )
            )
        } else {
            return MessageImageViewHolder(
                DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.rv_item_red_packet_img, parent, false
                )
            )
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val message: ReadPacketMessageBean = messages[position]
        if (0 == getItemViewType(position)) {
            (holder as MessageDataViewHolder).bind(message)
        } else if (1 == getItemViewType(position)) { //添加广告
            (holder as MessageAdViewHolder).bind(message)
        } else {
            (holder as MessageImageViewHolder).bindData(message, position)
        }

    }

    override fun getItemCount(): Int {
        return messages.size
    }


    fun sendMessage(bean: ReadPacketMessageBean) {
        messages.add(bean)
        notifyItemInserted(messages.size - 1)
    }

    fun updateMessage(bean: ReadPacketMessageBean?) {
        if (null == bean) {
            return
        }

        for ((index, itemBean) in messages.withIndex()) {
//            LogUtils.e(" 修改数据：bean.id == ${bean.id}  , itemBean.id == ${itemBean.id} ")
            //过滤数据
            if (bean.id == itemBean.id) {
                messages[index] = bean
            }
        }
        notifyDataSetChanged()
    }

    override fun getItemViewType(position: Int): Int {
        //发送消息类型 ： 0：正常消息 1：广告 2：红包
        return messages[position].messageType
    }


    // 定义视图元素
    inner class MessageDataViewHolder(var binding: RvItemRedPacketMsgBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(message: ReadPacketMessageBean) {
            // 使用消息数据设置视图元素
//            LogUtils.e("更新数据：$message")
            binding.tvUserMessage.text = message.sendMessage
            binding.tvUserName.text = message.userName

            Glide.with(binding.imgAvatar.context).load(message.avatar).into(binding.imgAvatar)

        }
    }

    inner class MessageImageViewHolder(var binding: RvItemRedPacketImgBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bindData(message: ReadPacketMessageBean, position: Int) {
            // 使用消息数据设置视图元素
//            LogUtils.e("更新数据：$message")

            binding.tvUserName.text = message.userName

            Glide.with(binding.imgAvatar.context).load(message.avatar).into(binding.imgAvatar)

            val time = System.currentTimeMillis() / 1000

            if (time - message.sendMessageTime > 10L) {
                Glide.with(binding.imgUserMessage.context)
                    .load(R.drawable.icon_video_red_packet_fase)
                    .into(binding.imgUserMessage)
            } else {
                Glide.with(binding.imgUserMessage.context)
                    .load(R.drawable.icon_video_red_packet_true)
                    .into(binding.imgUserMessage)
            }

            binding.imgUserMessage.setOnClickListener {
                if (time - message.sendMessageTime > 10L) {
                    com.jsnkj.basic.util.ToastUtils.show("该红包已被抢完，试试其他红包吧")
                } else {
                    click.onItemCallBack(position)
                }
            }

        }
    }


    // 定义视图元素
    inner class MessageAdViewHolder(var binding: RvItemRedPacketAdBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(message: ReadPacketMessageBean) {
            binding.tvUserName.text = message.userName

            Glide.with(binding.imgAvatar.context).load(message.avatar).into(binding.imgAvatar)
            binding.expressContainer.removeAllViews()
            var hjNativeAdData =  message.hjNativeAdData;
            hjNativeAdData!!.render()
            val expressAdView: View = hjNativeAdData!!.getExpressAdView()
            //媒体最终将要展示广告的容器（这里可以直接先addView,也可以在收到onRenderSuccess回调后addView）
            // 检查并移除任何已有的父视图
            val parent = expressAdView.parent
            if (parent != null) {
                (parent as ViewGroup).removeView(expressAdView)
            }
            binding.expressContainer.addView(expressAdView)
           // message.feedPosition?.showFeed(context, binding.expressContainer)
        }
    }

    interface onItemClickListener {
        fun onItemCallBack(position: Int)
    }

}